package com.qst.express.controller.wx;

import com.qst.commonUtils.result.R;
import com.qst.express.entity.User;
import com.qst.express.utils.UserUtil;
import com.qst.express.utils.wx.SignatureUtil;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author:CoolTiger
 * @title: QRCodeController
 * @projectName springboot-e
 * @description: TODO
 * @date:2022/10/13/14:55:34
 **/
@Api(description = "微信端-二维码")
@Controller
@RequestMapping("/e/wx")
public class QRCodeController {
    /**
     * 创建二维码
     * @param request
     * @param type
     * @param code
     * @return
     */
    @GetMapping("/createQrcode/{type}")
    public String createQrcode(HttpServletRequest request, @PathVariable(value = "type") String type, @RequestParam(name = "code",required = false) String code){
        String userPhone = null;
        String qRCodeContent = null;
        if ("express".equals(type)){
            //快递二维码：被扫后展示单个快递的信息
            //code
            qRCodeContent = "express_"+code;//二维码内容
        }else {
            //用户二维码：被扫后，快递员（柜子）端展示用户所有快递
            User wxUser = UserUtil.getWxUser(request.getSession());
            userPhone = wxUser.getUphone();
            qRCodeContent = "userPhone_"+userPhone;//二维码内容
        }
        HttpSession session =request.getSession();
        session.setAttribute("qrcode",qRCodeContent);//二维码内容存入session
        return "/e/personQRcode";//跳转到二维码页面，细节：使用@ResponseView可以直接返回跳转地址
    }

    /**
     * 获取二维码信息并展示
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/qrcode")
    @ResponseBody
    public R getQRCode(HttpServletRequest request,HttpServletResponse response){
        HttpSession session =request.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        if (qrcode ==null){
            return R.ok().code(-1).message("取件码获取出错，请用户重新操作");
        }else{
            return R.ok().code(0).data("qrcode",qrcode);
        }
    }

}
